﻿  function scripterLogger() {
    this.entries = {page: "",logs: [],  files: {}, errors: [] };
    this.log = function (logentry) {
     
    
        if (logentry.iserror !== undefined) {
 
            this.entries.errors.push(logentry);
            return;

        }

        if (logentry.fileid !== undefined) {       
            if (logentry.loadfile !== undefined)
            {            
                this.entries.files[logentry.fileid] = { "id": logentry.fileid, "name": logentry.text, logs: [], blocks: {} };
                this.entries.files[logentry.fileid].logs.push(logentry);
            }

            
            if (logentry.blockid !== undefined) {
                if (this.entries.files[logentry.fileid].blocks === undefined)
                    this.entries.files[logentry.fileid].blocks = {};
       

                if (logentry.beginblock !== undefined) {
                    this.entries.files[logentry.fileid].blocks[logentry.blockid] = { "id": logentry.blockid, "name": logentry.blockname, "description": logentry.blockDescription, logs: [], "loaded": false ,"binded":false,"triggered":false,  "errors": [] };
                }


                if (logentry.property_name !== undefined) {
                  
                    if (this.entries.files[logentry.fileid].blocks[logentry.blockid].properties === undefined)
                        this.entries.files[logentry.fileid].blocks[logentry.blockid].properties = [];

                    this.entries.files[logentry.fileid].blocks[logentry.blockid].properties.push({ "property_name": logentry.property_name,"property_value": logentry.property_value  });

                }


     
              
                if (logentry.rules !== undefined) {                   
                    this.entries.files[logentry.fileid].blocks[logentry.blockid]["rules"] = logentry.rules;
                }
            
                if (logentry.loaded !== undefined) {
                    
                    this.entries.files[logentry.fileid].blocks[logentry.blockid]["loaded"] = logentry.loaded;
                }
                
                if (logentry.binded !== undefined) {

                    this.entries.files[logentry.fileid].blocks[logentry.blockid]["binded"] = logentry.binded;
                }
                if (logentry.triggered !== undefined) {

                    this.entries.files[logentry.fileid].blocks[logentry.blockid]["triggered"] = logentry.triggered;
                }
            
                if (logentry.error !== undefined) {
                  
                    if (this.entries.files[logentry.fileid].blocks[logentry.blockid].errors === undefined)
                        this.entries.files[logentry.fileid].blocks[logentry.blockid].errors = [];

                    this.entries.files[logentry.fileid].blocks[logentry.blockid].errors.push({ "message": logentry.error });
                    
                }

                if (this.entries.files[logentry.fileid].blocks[logentry.blockid].properties === undefined)
                    this.entries.files[logentry.fileid].blocks[logentry.blockid].properties = [];

                this.entries.files[logentry.fileid].blocks[logentry.blockid].logs.push(logentry);
            }
        }
      
        this.entries.logs.push(logentry);

      
    }
    this.get = function () {
      
        return this.entries;
    }
    this.macros = {};
}

var loggertabs = {};

var logger = null;
function drawLoading(canvas) {
   
        // The draw() function runs every frame.
        // Documentation: http://www.clips.ua.ac.be/pages/pattern-canvas
        canvas.clear();
        fill(1, 0, 0, 1); // R,G,B,A (0.0-1.0)
        translate(200, 200);
        rotate(canvas.frame);
        rect(-75, -75, 150, 150);
        // Print some feedback (see the line below):
        print(canvas.mouse.x, canvas.mouse.y, "fps: " + canvas.fps);
   

}

function drawIconOnCanvas(w, opacity, canvas, enable) {
    if (canvas.getContext) {
        var c = canvas.getContext("2d");
        // name some constants
        canvas.width = canvas.height = w;
        c.clearRect(0, 0, w, w);
        var o = w / 12;
        var l = (w - 3 * o) / 2;
        // some drawing styles
        c.lineWidth = o / 4;
        c.lineCap = "round";
        c.lineJoin = "round";
        var wings = [
           { color: "rgba(0,  0,  0, " + opacity + ")" }, // red
           { color: "rgba(0, 0,  0, " + opacity + ")" }, // yellow
           { color: "rgba( 0,  0, 0, " + opacity + ")" }, // blue
           { color: "rgba( 0, 0,  0, " + opacity + ")" }, // green
        ];
        if(enable){
            wings = [
           { color: "rgba(255,  68,  34, " + opacity + ")" }, // red
           { color: "rgba(255, 204,  34, " + opacity + ")" }, // yellow
           { color: "rgba( 34,  68, 255, " + opacity + ")" }, // blue
           { color: "rgba( 68, 170,  68, " + opacity + ")" }, // green
            ];
        }
        c.translate(+w / 2, +w / 2);
        c.rotate(Math.PI);
        for (var i in wings) {
            var wing = wings[i];
            c.fillStyle = c.strokeStyle = wing.color;
            // draw a wing
            c.save();
            c.translate(+w / 4, +w / 4); c.rotate(Math.PI); c.translate(-w / 4, -w / 4);
            c.beginPath();
            c.moveTo(o, o);
            c.arc(o, o + l, l, -Math.PI / 2, 0, false);
            c.arc(o + l / 2, o + l, l / 2, 0, -Math.PI, true);
            c.lineTo(o, o);
            c.closePath();
            c.fill();
            c.stroke();
            c.restore();
            c.rotate(Math.PI / 2);
        }
        return c.getImageData(0, 0, 19, 19);
        //if (a)
        //    a.href = canvas.toDataURL();
    }
}

chrome.runtime.onMessage.addListener(
  function (request, sender, sendResponse) {
   
     
      var tabId = "";
     
      if (request.tab !== undefined)
          tabId = request.tab.id + "";
      else
          tabId = sender.tab.id + "";
    
          var tabKey = "TAB" + tabId;
        
          switch (request.command) {

              case "init":
             
                 
                  verifyLogger(tabKey);                
                  loggertabs[tabKey].entries = { page: request.commandValue.page, logs: [], files: {}, errors: [] };
                  loggertabs[tabKey].macros = request.commandValue.macros;
                  //return true;
                  break;
              case "add":
                
                  verifyLogger(tabKey);                  
                  loggertabs[tabKey].log(request.commandValue);
                 // return true;
                  break;
              case "add_property":
                 
                  verifyLogger(tabKey);
                  loggertabs[tabKey].log(request.commandValue);
                  // return true;
                  break;
                  

              case "get":          
                  if(loggertabs[tabKey]!== undefined)
                      sendResponse({ "result": { "entries": loggertabs[tabKey].entries, "macros": loggertabs[tabKey].macros } });
                   else
                       sendResponse({ "result": { "entries": null, "macros": null } });
                   return true;
                  break;
          }
        
          
 
   
 

 
  });

function verifyLogger(tabKey) {
    if (loggertabs[tabKey] == null || loggertabs[tabKey] === undefined) {
        loggertabs[tabKey] = new scripterLogger();
    }

}

 

chrome.tabs.onActivated.addListener(function (activeInfo) {
 

    var canvas = document.createElement('canvas');
 

    if (loggertabs["TAB" + activeInfo.tabId] !== undefined) {
          
            var imageData = drawIconOnCanvas(19, 1, canvas, true);
            chrome.browserAction.setIcon({
                imageData: imageData
            });

        }
        else {
            
            var imageData = drawIconOnCanvas(19, 1, canvas, false);
            chrome.browserAction.setIcon({
                imageData: imageData
            });
        }


    });
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
    if (changeInfo.status === 'complete') {
        if (loggertabs["TAB" + tabId] !== undefined) {
 
                //chrome.tabs.executeScript(tabId, {
                //    code: ' if (document.body.innerText.indexOf("Cat") !=-1) {' +
                //          '     alert("Cat not found!");' +
                //          ' }'
                //});

                var canvas = document.createElement('canvas');
                var imageData = drawIconOnCanvas(19, 1, canvas, true);
                chrome.browserAction.setIcon({
                    imageData: imageData
                });
            }
            else {
                var canvas = document.createElement('canvas');
                var imageData = drawIconOnCanvas(19, 1, canvas, false);
                chrome.browserAction.setIcon({
                    imageData: imageData
                });

            }
       

    }
    else {
        var canvas = document.createElement('canvas');
        var imageData = drawIconOnCanvas(19, 1, canvas, false);
        chrome.browserAction.setIcon({
            imageData: imageData
        });
    }
});


var canvas = document.createElement('canvas');
var imageData = drawIconOnCanvas(19, 1, canvas);
chrome.browserAction.setIcon({
    imageData: imageData
});